import os
import shutil
import traceback

from django.conf import settings
from django.http import JsonResponse

from XApiTestServer.utils.common import *
from XApiTestServer.utils.response import *


def getDebugtalk(request):
    dt = {}
    # debugtalk从文件中读取
    with open(os.path.join(settings.BASE_DIR, "debugtalk.py"), 'r', encoding='utf8') as pf:
        debugtalk = pf.read()
    dt['debugtalk'] = debugtalk
    response_body = SUCCESS_INFO
    response_body['data'] = dt
    return JsonResponse(response_body)

    # else:
    #     # 编辑完debugtalk后直接保存到文件，而不保存到数据库。
    #     debugtalk = request.POST.get('debugtalk')
    #     print(debugtalk)
    #     code = debugtalk.replace('new_line', '\n')
    #     with open(os.path.join(settings.BASE_DIR, "debugtalk.txt"), 'w') as pf:
    #         pf.write(code)
    #     dst_path = os.path.join(settings.BASE_DIR, "debugtalk.py")
    #     if os.path.exists(dst_path):
    #         os.remove(dst_path)
    #     os.rename(os.path.join(settings.BASE_DIR, "debugtalk.txt"), os.path.join(settings.BASE_DIR, "debugtalk.py"))
    #     return HttpResponseRedirect('/api/index')
